package io.bsly.modules.platform.controller;

import cn.hutool.json.JSONObject;
import io.bsly.common.utils.Constant;
import io.bsly.common.utils.R;
import io.bsly.modules.app.annotation.JwtIgnore;
import io.bsly.modules.app.entity.localEntity.DyOfficalOrderEntity;
import io.bsly.modules.app.service.localmaster.DyOfficalOrderService;
import io.bsly.modules.platform.api.douDianApi.DouDianConfigVo;
import io.bsly.modules.platform.api.douDianApi.DoudianHttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;

@Slf4j
@RestController
@RequestMapping("api/dy")
public class DouDianController {

    @Autowired
    DoudianHttpUtils doudianHttpUtils;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    DouDianConfigVo douDianConfig;
    @Autowired
    DyOfficalOrderService dyOfficalOrderService;
    @PostMapping("order")
    public R verifiedLink(@RequestBody Object body, HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        Enumeration<String> headerNames = request.getHeaderNames();
        if (headerNames.hasMoreElements()){
            String element = headerNames.nextElement();
            jsonObject.put(element, request.getHeader(element));
        }
        DyOfficalOrderEntity entity =new DyOfficalOrderEntity();
        entity.setShopName(body.toString());
        dyOfficalOrderService.save(entity);
        //TODO 信息推送，对订单进行保存
        log.warn("抖音平台推送信息--header kv  ，{}",jsonObject.toString());
        log.warn("抖音平台推送信息--body，{}",body.toString());
        return  R.ok();
    }



    @JwtIgnore
    @GetMapping("couponList")
    public R couponList(String orderId ,String certNo,HttpServletRequest request){
        try {
            R r = doudianHttpUtils.doudianCouponList(douDianConfig.getShopId(), orderId,certNo);
            return  R.ok();
        } catch (IOException e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @JwtIgnore
    @GetMapping("orderList")
    public R orderList(String startTime, String endTime ,HttpServletRequest request){
        try {
             doudianHttpUtils.doudianOrderList(douDianConfig.getShopId(),0,2,startTime,endTime);
            return  R.ok();
        } catch (IOException e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @JwtIgnore
    @GetMapping("token")
    public R getToken(HttpServletRequest request){
        try {
            String token = doudianHttpUtils.doudianAccessToken(douDianConfig.getShopId());
            return  R.ok(token);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @JwtIgnore
    @GetMapping("refreshToken")
    public R refresh_token(HttpServletRequest request) throws IOException{
        String refreshToken = (String)redisTemplate.opsForValue().get(Constant.DOUDIAN_REFRESH_TOKEN);
        String s = doudianHttpUtils.doudianRefreshToken(douDianConfig.getShopId(),refreshToken);
        return  R.ok(s);
    }

    @JwtIgnore
    @GetMapping("token2")
    public R getToken2(HttpServletRequest request){
        String s = doudianHttpUtils.doudianToken();
        return  R.ok(s);
    }
}
